/**
 * 
 */
package com.nari.heatshare.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.heatshare.model.ITempPointDataStatistics;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.platform.utils.StringUtils;
import com.nari.rescontroller.dao.IResourceAccessControlDao;

/**
 * 
 * @author Xiaopeng
 * @date 2014-3-17
 */
public class TempPointDataStatisticsDaoImpl extends GenericDaoImpl<ITempPointDataStatistics, String>
		implements ITempPointDataStatisticsDao {
	private IResourceAccessControlDao iResourceAccessControlDao;

	/**
	 * 通过集中器号、上传时间查询批次上传统计信息
	 * @param concentratorNo
	 * @param recordTime
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-17
	 */
	public ITempPointDataStatistics queryUniqueStatis(String concentratorNo, 
			String recordTime) throws Exception {
		String hsql = "from ITempPointDataStatistics tps " +
				"where tps.concentratorNo='"+concentratorNo+"' " +
				"and tps.recordTime='"+recordTime+"'";
		List<ITempPointDataStatistics> statisList = this.runQueryHSql(hsql);
		if(statisList!=null && statisList.size()>0) {
			return statisList.get(0);
		}
		return null;
	}
	
	/**
	 * 分页查询温度点数据统计信息
	 * @param concentratorNo
	 * @param recordTime
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-23
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<ITempPointDataStatistics> queryStatisPage(String concentratorNo, 
			String recordTime, PageCond pageCond) throws Exception {
		Criteria listCriteria = this.getCurrentSession().createCriteria(ITempPointDataStatistics.class);
		Criteria countCriteria = this.getCurrentSession().createCriteria(ITempPointDataStatistics.class);
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
		if(!"".equals(authorityRes)) {
			List<Object> projectUIDList = this.runQueryHSqlObject(authorityRes);
			String formatProjectUIDList = StringUtils.formatIDsByList(projectUIDList);
			formatProjectUIDList = StringUtils.formatIDListWithSyntax(formatProjectUIDList, ",", "'");
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			listCriteria.add(Restrictions.sqlRestriction("{alias}.t_concentratorNo in(select lower(t_concentrator_no) from BASE_CONCENTRATOR_INFO where t_project_id in("+formatProjectUIDList+"))"));
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_concentratorNo in(select lower(t_concentrator_no) from BASE_CONCENTRATOR_INFO where t_project_id in("+formatProjectUIDList+"))"));
		}
		//集中器号
		if (concentratorNo!=null && !"".equals(concentratorNo.trim())) {
			listCriteria.add(Restrictions.ilike("concentratorNo", concentratorNo.trim().toLowerCase(), MatchMode.ANYWHERE));
			countCriteria.add(Restrictions.ilike("concentratorNo", concentratorNo.trim().toLowerCase(), MatchMode.ANYWHERE));
		}
		//抄表时间
		if (recordTime!=null && !"".equals(recordTime.trim())) {
			listCriteria.add(Restrictions.eq("recordTime", recordTime.trim()));
			countCriteria.add(Restrictions.eq("recordTime", recordTime.trim()));
		}
		//统计结果集个数
		int total = ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
		//结果集排序
		listCriteria.addOrder(Order.desc("recordTime")).addOrder(Order.asc("concentratorNo"));
		//结果集分页
		listCriteria.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<ITempPointDataStatistics> statisPage = new ResultWithPageCond<ITempPointDataStatistics>();
		statisPage.setList(listCriteria.list());
		statisPage.setPageCond(pageCond);
		return statisPage;
	}

	/**
	 * @return the iResourceAccessControlDao
	 */
	public IResourceAccessControlDao getiResourceAccessControlDao() {
		return iResourceAccessControlDao;
	}

	/**
	 * @param iResourceAccessControlDao the iResourceAccessControlDao to set
	 */
	public void setiResourceAccessControlDao(
			IResourceAccessControlDao iResourceAccessControlDao) {
		this.iResourceAccessControlDao = iResourceAccessControlDao;
	}
	
}
